<?php
/**
 * Created by PhpStorm.
 * User: Quyet
 * Date: 12/22/2014
 * Time: 5:20 PM
 */

    if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['contQ'] && $_POST['answerItem']) {
    	include "../../db/dbConnect.php";

        $contQ=$_POST['contQ'];
        $ans = array();
        $ans = $_POST['answerItem'];
        $kind=$_POST['kind'];
        $code=0;
//check duplicate
        $sql="SELECT idpr_question from u_profile_question 
        						where question like N'".$contQ."' and deleted=0 
        						order by idpr_question desc 
        						limit 1";
		$re=mysql_query($sql);
		if(mysql_num_rows($re)){
			echo 'duplicate';
			
		}else{

			$sql="insert into u_profile_question (question)
        			values('".$contQ."')";
	        if(!mysql_query($sql)){
	        	echo mysql_error();
	        }else{
	        	$code++;
	        	$sql="SELECT idpr_question from u_profile_question 
	        						where question like N'".$contQ."' and deleted=0 
	        						order by idpr_question desc 
	        						limit 1";
				$re=mysql_query($sql);
				if(!mysql_error()){
					$row=mysql_fetch_array($re);
					$idQ=$row['idpr_question'];

					foreach ($ans as $key => $value) {
	        		# code...
		        		if ($value!='') {
		        			# code...
		        			$sql="insert into u_profile_answer (idquestion, answer)
		        			values(".$idQ.", '".$value."')";
		        			if(!mysql_query($sql)){
		        				echo mysql_error();
		        			}
		        			else $code++;
		        		}
	        		
	        		}
				}else echo mysql_error();
	        	
	        }
	        if ($code>=2) {
	        	# code...
	        	echo "success";
	        }

		}

        
    }else die();